Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
ethereumjs-common
Advanced tools
The ethereumjs-common package is a JavaScript library that provides common Ethereum constants, parameters, and functions. It is used to define and access chain and hardfork parameters, which are essential for Ethereum blockchain development and interaction.
Chain Parameters
This feature allows you to define and access chain parameters for different Ethereum networks. In this example, a custom chain based on the mainnet is created with specific network and chain IDs.
const Common = require('ethereumjs-common').default;
const mainnetCommon = Common.forCustomChain('mainnet', {
name: 'customchain',
networkId: 1,
chainId: 1
}, 'petersburg');
console.log(mainnetCommon.chainName());
Hardfork Parameters
This feature allows you to define and access hardfork parameters for different Ethereum networks. In this example, a custom chain based on the Ropsten testnet is created with the 'byzantium' hardfork.
const Common = require('ethereumjs-common').default;
const ropstenCommon = Common.forCustomChain('ropsten', {
name: 'customropsten',
networkId: 3,
chainId: 3
}, 'byzantium');
console.log(ropstenCommon.hardfork());
EIP Support
This feature allows you to check the EIPs (Ethereum Improvement Proposals) supported by a specific chain and hardfork. In this example, the EIPs supported by the 'istanbul' hardfork on the mainnet are logged.
const Common = require('ethereumjs-common').default;
const common = new Common({ chain: 'mainnet', hardfork: 'istanbul' });
console.log(common.eips());
Web3.js is a collection of libraries that allow you to interact with a local or remote Ethereum node using HTTP, IPC, or WebSocket. While it provides broader functionality for interacting with the Ethereum blockchain, it does not focus specifically on chain and hardfork parameters like ethereumjs-common.
Ethers.js is a library for interacting with the Ethereum blockchain and its ecosystem. It provides utilities for common tasks such as signing transactions and interacting with smart contracts. Similar to Web3.js, it offers a broader range of functionalities but does not specialize in chain and hardfork parameters.
Ethjs is a highly modular, lightweight library for interacting with the Ethereum blockchain. It provides basic functionalities for sending transactions and interacting with smart contracts. It is more lightweight compared to Web3.js and Ethers.js but does not focus on chain and hardfork parameters.
Resources common to all Ethereum implementations.
Succeeds the old ethereum/common library.
npm install ethereumjs-common
All parameters can be accessed through the Common
class which can be required through the
main package and instantiated either with just the chain
(e.g. 'mainnet') or the chain
together with a specific hardfork
provided.
Here are some simple usage examples:
const Common = require('ethereumjs-common')
// Instantiate with only the chain
let c = new Common('ropsten')
c.param('gasPrices', 'ecAddGas', 'byzantium') // 500
// Chain and hardfork provided
c = new Common('ropsten', 'byzantium')
c.param('pow', 'minerReward') // 3000000000000000000
// Access genesis data for Ropsten network
c.genesis().hash // 0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d
// Get bootstrap nodes for chain/network
c.bootstrapNodes() // Array with current nodes
It is encouraged to also explicitly set the supportedHardforks
if the initializing library
only supports a certain range of hardforks
:
let c = new Common('ropsten', null, ['byzantium', 'constantinople', 'petersburg'])
This will e.g. throw an error when a param is requested for an unsupported hardfork and like this prevents unpredicted behaviour.
See the API documentation for a full list of functions for accessing specific chain and
depending hardfork parameters. There are also additional helper functions like
paramByBlock (topic, name, blockNumber)
or hardforkIsActiveOnBlock (hardfork, blockNumber)
to ease blockNumber
based access to parameters.
There are currently parameter changes by the following past and future hardfork by the library supported:
chainstart
homestead
dao
tangerineWhistle
spuriousDragon
byzantium
constantinople
petersburg
(aka constantinopleFix
, apply together with constantinople
)istanbul
muirGlacier
The muirGlacier
HF delaying the difficulty bomb and scheduled for January 2020
is supported by the library since v1.5.0
.
For hardfork-specific parameter access with the param()
and paramByBlock()
functions
you can use the following topics
:
gasConfig
gasPrices
vm
pow
casper
sharding
See one of the hardfork files like byzantium.json
in the hardforks
directory
for an overview. For consistency, the chain start (chainstart
) is considered an own
hardfork.
The hardfork-specific json files only contain the deltas from chainstart
and
shouldn't be accessed directly until you have a specific reason for it.
Note: The list of gasPrices
and gas price changes on hardforks is consistent
but not complete, so there are currently gas price values missing (PRs welcome!).
Supported chains:
mainnet
ropsten
rinkeby
kovan
goerli
(final configuration since v1.1.0
)The following chain-specific parameters are provided:
name
chainId
networkId
genesis
block header valueshardforks
block numbersbootstrapNodes
listTo get an overview of the different parameters have a look at one of the chain-specifc
files like mainnet.json
in the chains
directory, or to the Chain
type in ./src/types.ts.
There are two ways to set up a common instance with parameters for a private/custom chain:
You can pass a dictionary - conforming to the parameter format described above - with your custom values in
the constructor or the setChain()
method for the chain
parameter.
You can base your custom chain's config in a standard one, using the Common.forCustomChain
method.
There is no separate config file for bootstrap nodes like in the old ethereum-common
library.
Instead use the common.bootstrapNodes()
function to get nodes for a specific chain/network.
Network-specific genesis files are located in the genesisStates
folder.
Due to the large file sizes genesis states are not directly included in the index.js
file
but have to be accessed directly, e.g.:
const mainnetGenesisState = require('ethereumjs-common/dist/genesisStates/mainnet')
Or by accessing dynamically:
const genesisStates = require('ethereumjs-common/dist/genesisStates')
const mainnetGenesisState = genesisStates.genesisStateByName('mainnet')
const mainnetGenesisState = genesisStates.genesisStateById(1) // alternative via network Id
See our organizational documentation for an introduction to EthereumJS
as well as information on current standards and best practices.
If you want to join for work or do improvements on the libraries have a look at our contribution guidelines.
FAQs
Resources common to all Ethereum implementations
The npm package ethereumjs-common receives a total of 36,406 weekly downloads. As such, ethereumjs-common popularity was classified as popular.
We found that ethereumjs-common demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.